﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using GTS.MiniBox.Entity;
using Pub.Class;

namespace GTS.MiniBox.Helper {
    /// <summary>
    /// Box请求日志操作类
    /// 
    /// 修改纪录
    ///     2013-04-03 版本：1.0 系统自动创建此类
    /// 
    /// </summary>
    public partial class RequestLogHelper {
        /// <summary>
        /// Box请求日志缓存多少秒 x 5
        /// </summary>
        public static int cacheSeconds = 1440;
        /// <summary>
        /// Box请求日志添加记录
        /// </summary>
        /// <param name="requestLog">Box请求日志实体类</param>
        /// <param name="delCache">添加成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>返回添加成功后的ID</returns>
        public static Int64 Insert(RequestLog requestLog, string dbkey = "", string[] delCache = null) {
            string strSql = new SQL().Insert(RequestLog._)
                .Value(RequestLog._RequestID, requestLog.RequestID)
                .Value(RequestLog._IMSI, requestLog.IMSI)
                .Value(RequestLog._CstCode, requestLog.CstCode)
                .Value(RequestLog._CityCode, requestLog.CityCode)
                .Value(RequestLog._OriginID, requestLog.OriginID)
                .Value(RequestLog._DeviceCode, requestLog.DeviceCode)
                .Value(RequestLog._LoginDate, requestLog.LoginDate)
                .Value(RequestLog._Status, requestLog.Status)
                .Value(RequestLog._Version, requestLog.Version)
                .Value(RequestLog._Compile, requestLog.Compile)
                .Value(RequestLog._PhoneID, requestLog.PhoneID)
                .Value(RequestLog._Box, requestLog.Box)
                .Value(RequestLog._IP, requestLog.IP)
                .ToString();
            strSql += ";select SCOPE_IDENTITY();";
            object objValue = dbkey.IsNullEmpty() ? Data.GetScalar(strSql) : Data.Pool(dbkey).GetScalar(strSql);
            if (objValue.IsAllNull()) return 0;
            Int64 value = objValue.ToBigInt();
            if (delCache.IsNull()) return value;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.RequestLogCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.RequestLogCache_" + s + ")" : s);
            }
            return value;
        }
        /// <summary>
        /// Box请求日志添加记录
        /// </summary>
        /// <param name="requestLog">Box请求日志实体类</param>
        /// <returns>返回添加成功后的ID</returns>
        public static Int64 Insert(RequestLog requestLog, string dbkey = "") {
            return Insert(requestLog, dbkey, null);
        }
        /// <summary>
        /// Box请求日志查询数据，带分页
        /// </summary>
        /// <param name="pageIndex">当前第几页，从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="totalRecords">返回总记录数</param>
        /// <param name="where">附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="order">排序字段，不加“order by”</param>
        /// <param name="fieldList">设置需要返回的字段</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时随机取连接key</param>
        /// <param name="pageEnum">使用哪种分页方式（not_in、max_top、top_top、row_number、mysql、oracle、sqlite）</param>
        /// <returns>返回实体记录集</returns>
        public static IList<RequestLog> SelectPageList(int pageIndex, int pageSize, out int totalRecords, string dbkey = "", string where = "", string order = "", string fieldList = "", PagerSQLEnum pageEnum = PagerSQLEnum.row_number) {
            totalRecords = 0;
            string cacheNameKey = "GTS.MiniBox.RequestLogCache_SelectPageList_{0}_{1}_{2}_{3}_{4}".FormatWith(pageIndex, pageSize, where, order, fieldList);
            string cacheRecordsKey = "GTS.MiniBox.RequestLogCache_RecordsSelectPageList_{0}_{1}_{2}_{3}_{4}".FormatWith(pageIndex, pageSize, where, order, fieldList);
            IList<RequestLog> list = (IList<RequestLog>)Cache2.Get(cacheNameKey);
            if (!list.IsNull()) { totalRecords = (int)Cache2.Get(cacheRecordsKey); return list; }

            using (PagerSQLHelper s = new PagerSQLHelper(pageEnum)) {
                PagerSql sql = s.GetSQL(pageIndex, pageSize, RequestLog._, RequestLog._RequestID, fieldList.IfNullOrEmpty("[RequestID],[IMSI],[CstCode],[CityCode],[OriginID],[DeviceCode],[LoginDate],[Status],[Version],[Compile],[PhoneID],[Box],[IP],"), where, "", order);
                DataSet ds = Data.Pool(dbkey).GetDataSet(sql.DataSql + ";" + sql.CountSql);
                if (ds.IsNull()) return list;
                list = ds.Tables[0].ToList<RequestLog>();
                totalRecords = ds.Tables[1].Rows[0][0].ToInt();
                ds.Dispose(); ds = null;
            }
            Cache2.Insert(cacheNameKey, list, cacheSeconds);
            Cache2.Insert(cacheRecordsKey, totalRecords, cacheSeconds);
            return list;
        }
        /// <summary>
        /// Box请求日志查询记录，带分页
        /// </summary>
        /// <param name="pageIndex">当前第几页，从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="totalRecords">返回总记录数</param>
        /// <returns>返回实体记录集</returns>
        public static IList<RequestLog> SelectPageList(int pageIndex, int pageSize, out int totalRecords, string dbkey = "") {
            return SelectPageList(pageIndex, pageSize, out totalRecords, dbkey, "", "", "", PagerSQLEnum.row_number);
        }
        /// <summary>
        /// 清除Box请求日志查询记录，带分页的缓存
        /// </summary>
        public static void ClearCacheSelectPageList() {
            string cacheNameKey = "GTS.MiniBox.RequestLogCache_SelectPageList_(.+?)";
            string cacheRecordsKey = "GTS.MiniBox.RequestLogCache_RecordsSelectPageList_(.+?)";
            Cache2.RemoveByPattern(cacheNameKey);
            Cache2.RemoveByPattern(cacheRecordsKey);
        }
    }
}

